package zhoug.zui.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;

import zhoug.zui.R;
import zhoug.zui.group.ZUIFrameLayout;
import zhoug.zui.ints.OnItemClickListener;

/**
 * 列表popupWindow
 * @Author 35574
 * @Date 2020/7/28
 * @Description
 */
public class ZUIListPopupWindow extends ZUIBasePopupWindow {
    /**
     * 数据适配器
     */
    private ListAdapter mListAdapter;
    private ZUIFrameLayout mContentView;
    private ListView mListView;
    private Drawable divider;
    private int dividerHeight;
    private OnItemClickListener mOnItemClickListener;

    public ZUIListPopupWindow(Context context) {
        this(context, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public ZUIListPopupWindow(Context context, int width, int height) {
        super(context, width, height);
        init();
    }

    protected void init() {
        mContentView = (ZUIFrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.zui_list_popup, null);
        mListView = mContentView.findViewById(R.id.listView);
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            dismiss();
            if(mOnItemClickListener!=null){
                mOnItemClickListener.onItemClick(position);
            }
        });
        setContentView(mContentView);
    }


    public ZUIListPopupWindow setListAdapter(ListAdapter listAdapter) {
        this.mListAdapter = listAdapter;
        mListView.setAdapter(mListAdapter);
        return this;
    }

    public ZUIListPopupWindow setDivider(Drawable divider){
        this.divider = divider;
        return this;
    }

    public ZUIListPopupWindow setDividerHeight(int dividerHeight){
        this.dividerHeight = dividerHeight;
        return this;
    }

    public ZUIListPopupWindow showDivider(boolean showDivider){
        if(showDivider){
            if(divider==null){
                divider=new ColorDrawable(Color.parseColor("#dddddd"));
            }
            if(dividerHeight==0){
                dividerHeight=1;
            }
            mListView.setDivider(divider);
            mListView.setDividerHeight(dividerHeight);
        }else{
            mListView.setDivider(null);
            mListView.setDividerHeight(0);
        }
        return this;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }


    public void setMaxHeight(int maxHeight){
        mContentView.setMaxHeight(maxHeight);
    }
}
